package tom.spring.cloud.consumer.controller;

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import tom.spring.cloud.consumer.service.HelloService;
import tom.spring.cloud.consumer.service.HelloService2;

import java.util.concurrent.ExecutionException;

/**
 * @author ZHUFEIFEI
 */
@RestController
@RequestMapping("hello")
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("test01")
    public String hello1(String name) {
        //无注册中心调用方式，配置文件eureka.client.enabled=false
        ResponseEntity<String> resp = this.restTemplate.exchange("http://localhost:8020/hello/{1}"
                , HttpMethod.GET, null, String.class, name);
        if (resp.getStatusCode() == HttpStatus.OK) {
            return resp.getBody();
        }
        return "error";
    }

    @GetMapping("test02")
    public String hello2(String name) {
        //使用EnableDiscoveryClient，注册中心，取服务名
        ResponseEntity<String> resp = this.restTemplate.exchange("http://provider01/hello/{1}"
                , HttpMethod.GET, null, String.class, name);
        if (resp.getStatusCode() == HttpStatus.OK) {
            return resp.getBody();
        }
        return "error";
    }

    @Autowired
    private HelloService helloService;

    /**
     * hystrixRequestCache只是缓存同一次请求中对同一cacheKey多次查询数据，仅第一次发送查询请求，后续从requestCache中取
     * @param name
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("test03")
    public String hello3(String name) throws ExecutionException, InterruptedException {
        //初始化线程ThreadLocal请求上下文，hystrix使用线程变量做缓存,实际应用中将hystrix上下文在过滤器中创建和销毁
//        HystrixRequestContext ctx = HystrixRequestContext.initializeContext();
        String info;
//        try {
            info = this.helloService.userDefinedCmd(name);
            this.helloService.userDefinedCmd(name);
            this.helloService.updateWithManual(name);
            this.helloService.userDefinedCmd(name);
//        } finally {
//            ctx.shutdown();
//        }
        return info;
    }

    @GetMapping("test04")
    public String hello4(String name) {
        String info = this.helloService.annotationMethod(name);
        this.helloService.annotationMethod(name);
        this.helloService.updateWithAnnotation(name);
        this.helloService.annotationMethod(name);
        return info;
    }

    @Autowired
    private HelloService2 helloService2;
    @GetMapping("test05")
    public String hello5(String name) throws ExecutionException, InterruptedException {
        return helloService2.helloWithCode(name);
    }

    @GetMapping("test06")
    public String hello6(String name) {
        return helloService2.helloWithAnnotation(name);
    }

}
